import Vue from 'vue'
import VueRouter from 'vue-router'
// 引入store
import store from '@/store'
// 引入路由中需要使用的组件
// import Login from '@/views/login/index.vue'
// import Layout from '@/views/layout/index.vue'
// import Home from '@/views/home/index.vue'
// 使用Vue引入VueRouter插件功能
Vue.use(VueRouter)

// 路由规则对象(添加需要认证的requiresAuth信息)
const routes = [
  {
    path: '/login',
    name: 'login',
    component: () => import(/* webpackChunkName:'login' */'@/views/login/index')
  },
  {
    path: '/',
    component: () => import(/* webpackChunkName:'layout' */'@/views/layout/index'),
    meta: { requiresAuth: true },
    children: [
      {
        path: '',
        name: 'home',
        component: () => import(/* webpackChunkName:'home' */'@/views/home/index')
      },
      {
        path: '/role',
        name: 'role',
        component: () => import(/* webpackChunkName:'role' */'@/views/role/index')
      },
      {
        path: '/menu',
        name: 'menu',
        component: () => import(/* webpackChunkName:'menu' */'@/views/menu/index')
      },
      {
        path: '/resource',
        name: 'resource',
        component: () => import(/* webpackChunkName:'resource' */'@/views/resource/index')
      },
      {
        path: '/user',
        name: 'user',
        component: () => import(/* webpackChunkName:'user' */'@/views/user/index')
      },
      {
        path: '/course',
        name: 'course',
        component: () => import(/* webpackChunkName:'course' */'@/views/course/index')
      },
      {
        path: '/course/create',
        name: 'course-create',
        component: () => import(/* webpackChunkName:'course-create' */'@/views/course/create')
      },
      {
        path: '/course/:courseId/edit',
        name: 'course-edit',
        component: () => import(/* webpackChunkName:'course-edit' */'@/views/course/edit'),
        props: true
      },
      {
        path: '/course/:courseId/section',
        name: 'course-section',
        component: () => import(/* webpackChunkName:'course-section' */'@/views/course/section'),
        props: true
      },
      {
        path: '/course/:courseId/video',
        name: 'course-video',
        component: () => import(/* webpackChunkName:'course-video' */'@/views/course/video'),
        props: true
      },
      {
        path: '/advert',
        name: 'advert',
        component: () => import(/* webpackChunkName:'advert' */'@/views/advert/index')
      },
      {
        path: '/advert-space',
        name: 'advert-space',
        component: () => import(/* webpackChunkName:'user' */'@/views/advert-space/index')
      },
      {
        path: '/menu/create',
        name: 'menu-create',
        component: () => import(/* webpackChunkName:'menu-creat' */'@/views/menu/menuCreate')
      },
      {
        path: '/menu/:id/edit',
        name: 'menu-edit',
        component: () => import(/* webpackChunkName:'menu-edit' */'@/views/menu/menuEdit')
      },
      {
        path: '/role/:roleId/alloc-menu',
        name: 'alloc-menu',
        props: true,
        component: () => import(/* webpackChunkName:'alloc-menu' */'@/views/role/alloc-menu')
      }
    ]
  },
  {
    path: '*',
    name: 'error-page',
    component: () => import(/* webpackChunkName:'error-page' */'@/views/error-page/index')
  }
]

const router = new VueRouter({
  routes
})
router.beforeEach((to, from, next) => {
  // 验证to路由是否需要身份验证
  if (to.matched.some(record => record.meta.requiresAuth)) {
    // console.log('当前页面需要认证')
    // 验证vuex的store中的登录信息是否存储
    if (!store.state.user) {
      // 未登录，跳转到login页面
      return next({ name: 'login', query: { redirect: to.fullPath } })
    }
    // 已经登录，允许访问
    next()
  } else {
    console.log('当前页面不需要认证')
    next()
  }
})
export default router
